<template>
  <view class="content" style="padding-left: 0; padding-right: 0">
    <uni-section title="tips" type="line" style="margin-top: 0"></uni-section>
    <text class="forms">{{ tipsText }}</text>
    <uni-section title="登记信息" type="line"></uni-section>
    <view class="forms">
      <!-- 学习登记 -->
      <uni-forms v-if="addFlag" ref="addForm" :modelValue="addPointData" :rules="addRules">
        <uni-forms-item v-if="addType === 'supervise'" label="姓名">
          <uni-data-picker v-model="addPointData.userId" :localdata="supervisedList" :map="listMap" :step-searh="false" placeholder="请选择被监督者"></uni-data-picker>
        </uni-forms-item>
        <uni-forms-item label="学习时长" name="registDuration">
          <uni-easyinput type="number" v-model="addPointData.registDuration" placeholder="请输入学习时长（分钟）" @input="handleTimesChange" />
        </uni-forms-item>
        <uni-forms-item :label="'获得' + (addType === 'supervise' ? '玉币' : '玻璃币')" name="points">
          <uni-easyinput type="number" disabled v-model="addPointData.points" />
        </uni-forms-item>
        <uni-forms-item label="备注" name="remark">
          <uni-easyinput type="textarea" v-model="addPointData.remark" />
        </uni-forms-item>
      </uni-forms>
      <!-- 使用登记 -->
      <uni-forms v-else :modelValue="usePointData">
        <uni-forms-item label="娱乐时长" name="times">
          <uni-easyinput type="number" v-model="usePointData.times" placeholder="请输入时长（分钟）" />
        </uni-forms-item>
        <uni-forms-item label="玉币时长" name="recognizedPointsRegistDuration">
          <uni-easyinput type="number" v-model="usePointData.recognizedPointsRegistDuration" placeholder="请输入使用时长（分钟）" @input="handleUseTimeChange1" />
        </uni-forms-item>
        <uni-forms-item label="使用玉币" name="recognizedPoints">
          <uni-easyinput type="number" disabled v-model="usePointData.recognizedPoints" />
        </uni-forms-item>
        <uni-forms-item label="玻璃币时长" name="privatePointsRegistDuration">
          <uni-easyinput type="number" v-model="usePointData.privatePointsRegistDuration" placeholder="请输入使用时长（分钟）" @input="handleUseTimeChange2" />
        </uni-forms-item>
        <uni-forms-item label="使用玻璃币" name="privatePoints">
          <uni-easyinput type="number" disabled v-model="usePointData.privatePoints" />
        </uni-forms-item>
        <uni-forms-item label="备注" name="remark">
          <uni-easyinput type="textarea" v-model="usePointData.remark" />
        </uni-forms-item>
      </uni-forms>
      <button @click="handleCommit">确认</button>
    </view>
  </view>
</template>

<script>
import { apiRegistAddRecord, apiRegistUseRecord } from '@/api/pointRegist.js'
import { apiGetSupervisedList } from '@/api/userRelation.js'

export default {
  data() {
    return {
      addFlag: true,
      addType: 'private',
      maxRecognizedPoints: 0,
      maxPrivatePoints: 0,
      tipsText: '请输入登记信息',
      supervisedList: [],
      listMap: { text: 'userName', value: 'userId' },
      index: 0,
      addPointData: {
        times: 0,
        registDuration: 0,
        points: 0,
        remark: ''
      },
      addRules: {
        registDuration: {
          rules: [
            {
              required: true,
              errorMessage: '请输入学习时长'
            },
            {
              maximum: 100,
              minimum: 0,
              errorMessage: '输入值必须大于0'
            }
          ]
        }
      },
      usePointData: {
        times: 0,
        recognizedPointsRegistDuration: 0,
        recognizedPoints: 0,
        privatePointsRegistDuration: 0,
        privatePoints: 0,
        remark: ''
      }
    }
  },
  onLoad(option) {
    this.addFlag = option.addFlag === 'true'
    this.addType = option.addType
    if (this.addType === 'supervise') {
      this.getSupervisedList()
    }
    if (!this.addFlag) {
      this.tipsText += `，当前可用玉币 ${option.recognizedPoints} 可抵扣时长 ${option.recognizedPoints * 3} 分钟`
      this.tipsText += `、可用玻璃币 ${option.privatePoints} 可抵扣时长 ${option.privatePoints * 3} 分钟`
    }
  },
  methods: {
    getSupervisedList() {
      apiGetSupervisedList().then(res => {
        this.supervisedList = res.data.result
      })
    },
    handleTimesChange(value) {
      this.addPointData.registDuration = parseInt(value)
      this.addPointData.points = parseInt(value / 3)
    },
    handleUseTimeChange1(value) {
      this.usePointData.recognizedPointsRegistDuration = parseInt(value)
      this.usePointData.recognizedPoints = Math.ceil(value / 3)
    },
    handleUseTimeChange2(value) {
      this.usePointData.privatePointsRegistDuration = parseInt(value)
      this.usePointData.privatePoints = Math.ceil(value / 3)
    },
    handleCommit() {
      if (this.addFlag) {
        this.addCommit()
      } else {
        this.useCommit()
      }
    },
    addCommit() {
      const paramObj = {
        registType: this.addType === 'supervise' ? 1 : 0,
        userId: this.addType === 'supervise' ? 'a' : undefined,
        ...this.addPointData
      }
      paramObj.registDuration = parseInt(paramObj.registDuration)
      apiRegistAddRecord(paramObj)
        .then(res =>
          uni.showToast({
            title: '登记成功',
            success() {
              setTimeout(function () {
                uni.reLaunch({ url: '../main/main?refresh=true' })
              }, 1500)
            }
          })
        )
        .catch(err =>
          uni.showToast({
            icon: 'error',
            title: res.data.message
          })
        )
    },
    useCommit() {
      const paramObj = {
        privatePointsRegistDuration: this.usePointData.privatePointsRegistDuration ? parseInt(this.usePointData.privatePointsRegistDuration) : 0,
        privatePoints: this.usePointData.privatePoints ?? 0,
        recognizedPointsRegistDuration: this.usePointData.recognizedPointsRegistDuration ? parseInt(this.usePointData.recognizedPointsRegistDuration) : 0,
        recognizedPoints: this.usePointData.recognizedPoints ?? 0,
        remark: this.usePointData.remark
      }
      apiRegistUseRecord(paramObj)
        .then(() =>
          uni.showToast({
            title: '登记成功',
            success() {
              setTimeout(function () {
                uni.reLaunch({ url: '../main/main?refresh=true' })
              }, 1500)
            }
          })
        )
        .catch(() =>
          uni.showToast({
            icon: 'error',
            title: res.data.message
          })
        )
    }
  }
}
</script>

<style>
.forms {
  background-color: #fff;
  padding: 20rpx 20rpx;
}
</style>
